package com.bckj.fastboot.iot.data.mysql.repository;

import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.bckj.fastboot.core.lang.model.PageResult;
import com.bckj.fastboot.core.page.PageSupport;
import com.bckj.fastboot.iot.common.dto.query.IotDeviceStatusRecordQuery;
import com.bckj.fastboot.iot.common.dto.request.IotDeviceStatusRecordRequest;
import com.bckj.fastboot.iot.common.dto.response.IotDeviceStatusRecordResponse;
import com.bckj.fastboot.iot.common.repository.IotDeviceStatusRecordRepository;
import com.bckj.fastboot.iot.data.mysql.convert.IotDeviceStatusRecordConvert;
import com.bckj.fastboot.iot.data.mysql.entity.IotDeviceStatusRecord;
import com.bckj.fastboot.iot.data.mysql.mapper.IotDeviceStatusRecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * IotDeviceStatusRecordRepository
 *
 * @author wfc
 **/
@Repository
@RequiredArgsConstructor
public class IotDeviceStatusRecordRepositoryImpl
        extends CrudRepository<IotDeviceStatusRecordMapper, IotDeviceStatusRecord>
        implements IotDeviceStatusRecordRepository {

    private final IotDeviceStatusRecordConvert iotDeviceStatusRecordConvert;

    @Override
    public List<IotDeviceStatusRecordResponse> findList(IotDeviceStatusRecordQuery query) {
        return getBaseMapper().findList(query);
    }

    @Override
    public PageResult<IotDeviceStatusRecordResponse> page(Pageable page, IotDeviceStatusRecordQuery query) {
        var result = PageSupport.page(page, query, this::findList);
        return PageResult.of(result);
    }

    @Override
    public boolean save(IotDeviceStatusRecordRequest request) {
        var entity = iotDeviceStatusRecordConvert.toEntity(request);
        return save(entity);
    }
}
